package com.joker.web.controller.orange;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.joker.common.annotation.RepeatSubmit;
import com.joker.common.annotation.Log;
import com.joker.common.core.controller.BaseController;
import com.joker.common.core.domain.PageQuery;
import com.joker.common.core.domain.R;
import com.joker.common.core.validate.AddGroup;
import com.joker.common.core.validate.EditGroup;
import com.joker.common.enums.BusinessType;
import com.joker.common.utils.poi.ExcelUtil;
import com.joker.orange.domain.vo.VideoInfoVo;
import com.joker.orange.domain.bo.VideoInfoBo;
import com.joker.orange.service.IVideoInfoService;
import com.joker.common.core.page.TableDataInfo;

/**
 * 视频信息
 *
 * @author Joker
 * @date 2024-06-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/resource/video")
public class VideoInfoController extends BaseController {

    private final IVideoInfoService iVideoInfoService;

    /**
     * 查询视频信息列表
     */
    @SaCheckPermission("resource:video:list")
    @GetMapping("/list")
    public TableDataInfo<VideoInfoVo> list(VideoInfoBo bo, PageQuery pageQuery) {
        return iVideoInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出视频信息列表
     */
    @SaCheckPermission("resource:video:export")
    @Log(title = "视频信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(VideoInfoBo bo, HttpServletResponse response) {
        List<VideoInfoVo> list = iVideoInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "视频信息", VideoInfoVo.class, response);
    }

    /**
     * 获取视频信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("resource:video:query")
    @GetMapping("/{id}")
    public R<VideoInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iVideoInfoService.queryById(id));
    }

    /**
     * 新增视频信息
     */
    @SaCheckPermission("resource:video:add")
    @Log(title = "视频信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VideoInfoBo bo) {
        return toAjax(iVideoInfoService.insertByBo(bo));
    }

    /**
     * 修改视频信息
     */
    @SaCheckPermission("resource:video:edit")
    @Log(title = "视频信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VideoInfoBo bo) {
        return toAjax(iVideoInfoService.updateByBo(bo));
    }

    /**
     * 删除视频信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("resource:video:remove")
    @Log(title = "视频信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iVideoInfoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 修改视频信息
     */
    @SaCheckPermission("resource:video:edit")
    @Log(title = "视频信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateBatchVideo")
    public R<Void> updateBatchVideo(@RequestBody VideoInfoBo bo) {
        if (ObjectUtil.isEmpty(bo.getChargeType())) {
            return R.fail("小说收费类型为必须填写");
        }
        if ("2".equals(bo.getChargeType()) && ObjectUtil.isEmpty(bo.getConsumeDiamond())) {
            return R.fail("收费类型为钻石收费，消费钻石必须填写");
        }
        iVideoInfoService.updateBatchVideo(bo);
        return R.ok();
    }

    /**
     * 修改视频信息
     */
    @SaCheckPermission("resource:video:edit")
    @Log(title = "视频信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateBatchUserVideo")
    public R<Void> updateBatchUserVideo(@RequestBody VideoInfoBo bo) {
        iVideoInfoService.updateBatchUserVideo(bo);
        return R.ok();
    }
}
